const express = require('express')
const empmodels = require('../models/employee')
const router = express.Router()

//查询用户
router.get('/selectemploy', function (req, res) {
    empmodels.selectemploy().then(value => {
        if (value.length > 0) {
            res.send({
                data: {
                    code: 200,
                    msg: "查询成功",
                    employlist: value
                }
            })
        } else {
            res.send({
                data: {
                    code: 201,
                    msg: "查询失败"
                }
            })
        }
    }, reason => {
        res.send({
            reason
        })
    })
})

//新建员工信息
router.post('/addemploy', function (req1, res) {
    let { avatar, username, password, realname, role } = req1.body
    if (username == null || password == null || realname == null || role == null) {
        res.send({
            data: {
                code: 203,
                msg: "参数错误"
            }
        })
    } else {
        let data = [
            avatar,
            username,
            password,
            realname,
            role
        ]
        empmodels.addemploy(data).then(value => {
            if (value.affectedRows > 0) {
                res.send({
                    data: {
                        code: 200,
                        msg: "新建成功"
                    }
                })
            } else {
                res.send({
                    data: {
                        code: 201,
                        msg: "新建失败"
                    }
                })
            }
        }, reason => {
            res.send({
                reason
            })
        })
    }

})

//删除员工信息
router.post('/deleteemploy', function (req, res) {
    let { id } = req.body
    let data = [id]
    empmodels.deleteemploy(data).then(value => {
        if (value.affectedRows > 0) {
            res.send({
                data: {
                    code: 200,
                    msg: "删除成功"
                }
            })
        } else {
            res.send({
                data: {
                    code: 201,
                    msg: "删除失败"
                }
            })
        }
    }, reason => {
        res.send({
            reason
        })
    })
})

//编辑员工信息
router.post('/updateemploy', function (req, res) {
    let { avatar, realname, role, password, id } = req.body
    let data = [ avatar, realname, role, password, id]
    empmodels.updateemploy(data).then(value => {
        if (value.affectedRows > 0) {
            res.send({
                data: {
                    code: 200,
                    msg: "编辑成功"
                }
            })
        } else {
            res.send({
                data: {
                    code: 201,
                    msg: "编辑失败"
                }
            })
        }
    }, reason => {
        res.send({
            reason
        })
    })
})

//删除多条员工信息
router.post('/deletemoreemploy', function (req, res) {
    let data = req.body
    if (!Array.isArray(data)) {
        res.send({
            data: {
                status: 0,
                message: '必须是数组'
            }
        })
    } else if (data == '') {
        res.send({
            data: {
                status: 0,
                message: '参数不能为空'
            }
        })
    } else {
        empmodels.deletemoreemploy(data).then(value => {
            if (value.affectedRows > 0) {
                res.send({
                    data: {
                        code: 200,
                        msg: "删除成功"
                    }
                })
            } else {
                res.send({
                    data: {
                        code: 201,
                        msg: "删除失败"
                    }
                })
            }
        }, reason => {
            res.send({
                reason
            })
        })
    }

})





module.exports = router